<?php use_helper('Date'); ?>
<?php require('../lib/utils.php'); ?>

<h1>Planning</h1>

<div class="action">
<?php echo link_to('Saisie prévisions', 'planning/new_prev', array('class' => 'action')) ?>
&nbsp;
<?php echo link_to('Saisie consommé', 'planning/new_charge', array('class' => 'action')) ?>
</div>

<?php
	// constitution du tableau
	$semaine_min = 99990101;
	$semaine_max = 0;
	$charge_list = array();
	foreach ($planning_phases as $phase) {
		foreach ($phase->getTachesPlanning() as $tache) {
			$cumul_prevu = 0;
			$cumul_consomme = 0;
			foreach ($tache->getChargesPlanning() as $charge) {
				if (format_date($charge->getPlnchgDate(), 'yyyyMMdd') < $semaine_min) $semaine_min = format_date($charge->getPlnchgDate(), 'yyyyMMdd');
				if (format_date($charge->getPlnchgDate(), 'yyyyMMdd') > $semaine_max) $semaine_max = format_date($charge->getPlnchgDate(), 'yyyyMMdd');
				if (!isset($charge_list[$tache->getPlntacId()])) 
					$charge_list[$tache->getPlntacId()] = array();
				$cumul_prevu += $charge->getPlnchgChargePrevue();
				$cumul_consomme += $charge->getPlnchgChargeConsommee();
				$charge_list[$tache->getPlntacId()][format_date($charge->getPlnchgDate(), 'yyyy-MM-dd')] = array(
					'prevu' => $charge->getPlnchgChargePrevue(), 
					'consomme' => $charge->getPlnchgChargeConsommee(),
					'ecart' => $charge->getPlnchgChargePrevue() - $charge->getPlnchgChargeConsommee(),
					'cumul_prevu' => $cumul_prevu, 
					'cumul_consomme' => $cumul_consomme, 
				);
			}
		}
	}
	
	if ($semaine_max > 0) {
		$dt_min = new DateTime(sprintf('%s-%s-%s', substr($semaine_min, 0, 4), substr($semaine_min, 4, 2), substr($semaine_min, 6, 2)));
		$dt_max = new DateTime(sprintf('%s-%s-%s', substr($semaine_max, 0, 4), substr($semaine_max, 4, 2), substr($semaine_max, 6, 2)));
		$di = $dt_min->diff($dt_max);
		$nb_semaines = floor($di->format('%a') / 7) +1;
	} else {
		$nb_semaines = 0;
	}
?>


<table class="liste">
	<thead>
		<tr>
			<th/>
			<th>Libellé</th>
			<th>Début</th>
			<th>Echéance</th>
			<th class="l">Estimé</th>
			<th>Consommé</th>
			<th>Restant</th>
			<?php
			if ($nb_semaines > 0) {
				$dt = new DateTime($dt_min->format('Y-m-d'));
				for ($s=0; $s<$nb_semaines; $s++){
					echo '<th colspan="3" class="l r semaine">'. $dt->format('d/m') .'</th>';
					$dt->add(new DateInterval('P7D'));
				}
			}
			?>
		</tr>
	</thead>
	<tbody>
		<?php 
		foreach ($planning_phases as $phase) {
			// affichage de la phase
			echo '<tr class="sstitre">';
			echo '<td class="action">';
			echo link_to(image_tag('show.png'), 'planning/show_phase?plnphs_id='.$phase->getPlnphsId(), array('title' => 'Détail de la phase'));
			echo link_to(image_tag('edit.png'), 'planning/edit_phase?plnphs_id='.$phase->getPlnphsId(), array('title' => 'Modifier la phase'));
			echo link_to(image_tag('new.png'), 'planning/new_tache?phs_id='.$phase->getPlnphsId(), array('title' => 'Nouvelle tâche'));
			echo '</td>';
			echo '<td>'. $phase->getPlnphsLibelle() .'</td>';
			echo '<td>'. format_date($phase->getPlnphsDateDebut(), 'dd/MM/yyyy') .'</td>';
			echo '<td>'. format_date($phase->getPlnphsDateEcheance(), 'dd/MM/yyyy') .'</td>';
			echo '<td class="l nb">'. sprintd($phase->getPlnphsChargeEstimee()) .'</td>';
			echo '<td class="nb">'. sprintd($phase->getPlnphsChargeConsommee()) .'</td>';
			$solde = $phase->getPlnphsChargeEstimee() - $phase->getChargeConsommee();
			$class_solde = ($solde < 0 ? 'planning-alerte' : 'planning-ok');
			echo '<td class="r nb"><span class="'.$class_solde.'">'. sprintd($solde) .'</span></td>';
			for ($s=0; $s<$nb_semaines; $s++){
				echo '<td colspan="3" class="l r"></td>';
			}
			echo '</tr>';
			
			// affichage des taches de la phase
			foreach ($phase->getTachesPlanning() as $tache) {
				echo '<tr>';
				echo '<td class="action">';
				echo link_to(image_tag('show.png'), 'planning/show_tache?plntac_id='.$tache->getPlntacId(), array('title' => 'Détail de la tâche'));
				echo link_to(image_tag('edit.png'), 'planning/edit_tache?plntac_id='.$tache->getPlntacId(), array('title' => 'Modifier la tâche'));
				echo '</td>';
				echo '<td>'. $tache->getplntacLibelle() .'</td>';
				echo '<td>'. format_date($tache->getPlntacDateDebut(), 'dd/MM/yyyy') .'</td>';
				echo '<td>'. format_date($tache->getPlntacDateEcheance(), 'dd/MM/yyyy') .'</td>';
				echo '<td class="l nb">'. sprintd($tache->getPlntacChargeEstimee()) .'</td>';
				echo '<td class="nb">'. sprintd($tache->getPlntacChargeConsommee()) .'</td>';
				$solde = $tache->getPlntacChargeEstimee() - $tache->getPlntacChargeConsommee();
				$class_solde = ($solde < 0 ? 'planning-alerte' : 'planning-ok');
				echo '<td class="r nb"><span class="'.$class_solde.'">'. sprintd($solde) .'</span></td>';
				
				// affichage des prévisions
				$dt = new DateTime($dt_min->format('Y-m-d'));
				for ($s=0; $s<$nb_semaines; $s++){
					if (isset($charge_list[$tache->getPlntacId()][$dt->format('Y-m-d')])) {
						$charge = $charge_list[$tache->getPlntacId()][$dt->format('Y-m-d')];
						echo '<td class="l nb">'. sprintd($charge['prevu']) .'</td>';
						echo '<td class="nb">'. sprintd($charge['consomme']) .'</td>';
						$class_ecart = ($charge['ecart'] < 0 ? 'planning-alerte' : 'planning-ok');
						echo '<td class="nb r"><span class="'.$class_ecart.'">'. sprintd($charge['ecart']) .'</span></td>';
						// echo '<td class="nb">'. sprintd($charge['cumul_prevu']) .'</td>';
						// echo '<td class="nb r">'. sprintd($charge['cumul_consomme']) .'</td>';
					} else {
						echo '<td class="l"/><td colspan="1"/><td class="r"/>';
					}
					$dt->add(new DateInterval('P7D'));
				}
				echo '</tr>';
				
			}
		} 
		?>
	</tbody>
</table>



<div class="action">
	<?php echo link_to('Nouvelle phase', 'planning/new_phase', array('class' => 'action')) ?>
</div>


	<?php
		foreach ($user_list as $user) {
			echo $user->getUsername().'<br/>';
		}
	?>
